Skip to content

Compile SIMD (v128) with static helpers - #315

Draft
andreaTP wants to merge 2 commits into
bytecodealliance:mainfrom
andreaTP:compiler-simd
Draft

andreaTP wants to merge 2 commits into
bytecodealliance:mainfrom
andreaTP:compiler-simd

Conversation

@andreaTP

Copy link
Copy Markdown
Contributor

Summary

The runtime and build-time compilers now run WebAssembly SIMD. Until now, any module with a v128 op could only run in the interpreter (#205).

  • Representation. A v128 is two longs, low word first. That holds on the operand stack, in locals and in method parameters.
    • Results that include a v128 take the existing long[] multi-value path.
    • In the long[] call ABI a v128 takes two entries, as in the interpreter.
  • Ops.
    • Each compute op is an invokestatic of a V128Words helper, one call per 64-bit word of the result. This follows the OpcodeImpl pattern.
    • Shuffles, and ops that take one JVM instruction per word, are emitted inline. Those are v128 bitwise and i64x2 add/sub/mul/neg/shifts/lanes.
  • Fails loudly. Class init throws if SimdOp doesn't register every 0xFD opcode, or if any op lacks exactly one @OpCodeIdentifier helper.
  • No new requirements. Generated classes stay at Java 11, with no new flag or dependency.
  • Docs. There is a new SIMD section in the execution docs, and the README's roadmap line links to it. Both tell users to build their module with and without SIMD and measure both.

The first commit is a separate test-gen fix. Before it, v128 results with i64 or f64 lanes were never asserted, about 10k checks across the SIMD wasts.

Testing

  • All 58 simd_*.wast spec files now run against compiled code. Before, they were excluded.
  • SimdOpDifferentialTest runs each of the 192 compute ops compiled and interpreted, on edge cases and 2,000 random operands.
  • SimdBoundaryTest passes v128 values across:
    • compiled, interpreted and host calls;
    • call_indirect, call_ref and tail calls;
    • multi-value results with GC refs;
    • globals and exception payloads;
    • select and br_table;
    • split functions, in direct and trampoline mode.
  • BasicSimdTest moves to machine-tests, so it runs on both engines. That covers the check that a trapping store writes nothing.
  • ManyParamsAbiTest adds v128 parameters around the 255-slot boxing threshold.
  • Full mvn install: green on JDK 21. The compiler suites are also green on JDK 11.

Investigation

We compared four ways to compile v128 on JDK 25. The payloads were libsodium (argon2id, aegis256), OpenSSL ChaCha20, and small byte, float and ChaCha20 kernels, each built with and without -msimd128:

  1. Vector API (jdk.incubator.vector);
  2. inline scalar bytecode on two longs;
  3. static helpers;
  4. the hybrid in this PR.

The Vector API is fast in places but fragile:

  • It wins on the byte kernel, 1.8× faster than scalar.
  • It is 2× slower on OpenSSL ChaCha20.
  • On argon2id it is 10× slower. C2 hits LiveNodeCountInliningCutoff, and the vectors get boxed at 7.7 GB/op.
  • Changing only the method splitter's chunk size swings argon2id from 1,635 ms/op to 136, then back to about 4,100. JDK 21 allocates in more cases still.

It doesn't reach the hand-written numbers either. For the same row-vector ChaCha20, hand-written Vector API Java runs in 0.116 ms. Compiled from wasm with the Vector API backend, it takes 0.225 ms, slower than the plain wasm build at 0.198 ms. Compiled code reaches memory through the Memory interface, and those calls stay out of line next to vector code. The memory path is the bigger lever for every backend.

Static helpers perform about like inline scalar. They come within about 6% on the crypto payloads, with much less code: plain Java helpers instead of about 3,400 lines of ASM emitters.

Calling a helper for every op was too slow. C2 hit DesiredMethodLimit in large functions, and argon2id ran 4.4× slower than plain. Moving shuffles and the one-instruction ops inline fixed it, and that hybrid is what this PR ships.

This PR against the same source built without SIMD (JDK 25; the machine was noisy, so compare ratios):

payload SIMD build vs plain build
OpenSSL ChaCha20 2.5× faster
byte kernel 2.6× faster
libsodium aegis256 1.3× faster
libsodium argon2id on par
ChaCha20 kernel (vectorised throughout) 1.9× slower
float kernel 2× slower

SIMD builds win where clang turned byte loops into 16-byte operations. They lose where 32-bit lane rotates or float lanes are emulated on 64-bit words; plain scalar wasm gets native rotate instructions there.

Why scalar helpers first

  • Correctness and coverage first. Every SIMD module now compiles and passes the spec suite. Before, it fell back to the interpreter.
  • No platform cost. The Vector API would need JDK 21+, --add-modules jdk.incubator.vector, and an incubator API that still changes. This keeps Java 11 and zero flags.
  • Predictable. A helper that C2 doesn't inline costs a call, never an allocation. The Vector API's worst cases were 10–30× slowdowns that depend on splitter settings and the JDK version.
  • Doesn't block what comes next. A Vector API path, or faster memory access, can build on the same two-long representation and ABI.

Follow-ups (not in this PR)

  • The interpreter calls V128Words instead of its own V128Ops.
  • JMH SIMD benchmarks and a payload module (libsodium, OpenSSL, the kernels).
  • The splitter accounts for helper calls, and cuts at empty-stack points.
  • Peepholes: i8x16.shuffle as a 32-bit lane rotate, and rotate folding.
  • A faster linear-memory path for compiled code, scalar and v128.

v128 results with i64 or f64 lanes were never asserted (10,150 checks
across the SIMD wasts), and v128 assertions always read the start of the
results array instead of the result's own slots. Results are now indexed
by slot (two per v128), and i64 lane values accept unsigned decimals.
The compiler used to reject any module with a v128 op. A v128 is now two
longs, low word first, on the operand stack, in locals and in method
parameters; results with a v128 take the long[] multi-value path, and the
long[] call ABI holds a v128 as two entries, as the interpreter does.

Compute ops call V128Words static helpers, one per 64-bit word of the
result. Shuffles and ops that are one JVM instruction per word (v128
bitwise, i64x2 add/sub/mul/neg/shifts/lanes) are emitted inline, since
with every op a helper C2 hits DesiredMethodLimit in large SIMD functions.
SimdOp and V128Emitter check at class init that every v128 opcode is
registered and has exactly one helper. Compiled classes stay Java 11.

All SIMD spec wasts now run against compiled code. New tests compare each
op with the interpreter and cover v128 across calls, host imports,
globals, tags, tail calls, select/br_table and split functions.
BasicSimdTest moves to machine-tests to run on both engines.

The docs tell users to compare against a build without SIMD, since code
vectorised throughout can run about 2x slower than its plain build.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant